package com.network.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.network.pojo.User;
import com.network.pojo.UserInfo;
import com.network.service.UserInfoService;
import com.network.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
public class ManagerController {


    @Autowired
    UserService userService;

    @Autowired
    UserInfoService userInfoService;

    @GetMapping("/manager/acc")
    public String getAccountView() {
        return "manage/manageAccount";
    }


    @ResponseBody
    @GetMapping("/manager/account")
    public PageInfo<User> getAccount(@RequestParam("date") String date
                           , @RequestParam("page") int page
                           , @RequestParam("limit") int limit) {

        PageHelper.startPage(page, limit);
        List<User> allUser = userService.getAllUser(new Date(date));
        PageInfo<User> result = PageInfo.of(allUser);

        return result;
    }


    @GetMapping("/manager/update")
    public String updateShow(Model model,@RequestParam("username") String username
            ,@RequestParam("password")String password){

        model.addAttribute("username",username);
        model.addAttribute("password",password);
        return "manage/modify";
    }


    @ResponseBody
    @PutMapping("/manager/update")
    public String update(@RequestParam("username") String username
            ,@RequestParam("password") String password) {
        userService.updateUserByUsername(username,password);

        return password;
    }

    @ResponseBody
    @DeleteMapping("/manager/delete")
    public String delete(@RequestParam("username") String username) {


        userService.deleteUserByUsername(username);
        return "success";
    }


    @GetMapping("/manager/userI")
    public String getUserInfoView() {
        return "manage/manageUserInfo";
    }

    @ResponseBody
    @GetMapping("/manager/userInfo")
    public PageInfo<UserInfo> getUserInfo(@RequestParam("date") String date
            , @RequestParam("page") int page
            , @RequestParam("limit") int limit) {

        PageHelper.startPage(page,limit);
        List<UserInfo> allUserInfo = userInfoService.getAllUserInfo(new Date(date));
//        System.out.println(allUserInfo);
        PageInfo<UserInfo> of = PageInfo.of(allUserInfo);
        return of;
    }


}
